Public Class AttributeCleanup

    Private _AttributeCleanupResultDetails As AttributeCleanupResultDetailList

    Public Sub New()

    End Sub

    Public ReadOnly Property AttributeCleanupResultDetails() As AttributeCleanupResultDetailList
        Get
            Return _AttributeCleanupResultDetails
        End Get
    End Property

    Public Function ProcessCleans(ByVal CleanupList As AttributeCleanupList, ByVal IsEvaluationOnly As Boolean) _
        As AttributeCleanupResultList

        If _AttributeCleanupResultDetails Is Nothing Then
            Me._AttributeCleanupResultDetails = New AttributeCleanupResultDetailList
        End If

        Dim acrl As AttributeCleanupResultList = New AttributeCleanupResultList
        Dim acr As AttributeCleanupResult

        Dim acs As AttributeCleanupSet
        For Each acs In CleanupList
            acr = ProcessAClean(acs, IsEvaluationOnly)
            acrl.Add(acr)
        Next

        Return acrl

    End Function

    Private Function ProcessAClean(ByVal CleanupSet As AttributeCleanupSet, ByVal IsEvaluationOnly As Boolean) As AttributeCleanupResult

        'get the ad helper
        Dim myAD As ADHelper = ADHelper.GetInstance

        Dim groupFilter As String
        If My.Settings.UserManagementTargetGroupCN.Length > 0 Then
            groupFilter = "CN=" & My.Settings.UserManagementTargetGroupCN & "," & My.Settings.LDAPOUDN
        Else
            groupFilter = ""
        End If

        'setup a filter for the current value
        Dim filter As String = "(@AttributeName=@CurrentValue)"
        filter = filter.Replace("@AttributeName", CleanupSet.AttributeName)
        filter = filter.Replace("@CurrentValue", CleanupSet.CurrentValue)

        'what properties do we want in the resulting dataset
        Dim properties As String = "cn"

        'get the list of accounts (all we really need is the count, but...)
        Dim dt As DataTable
        dt = myAD.getAccounts(myAD.LDAPRoot, "user", groupFilter, properties, filter)

        Dim iCleanCount As Integer = 0
        Dim isCleaned As Boolean = False
        Dim cleanComments As String = ""

        Dim acrd As AttributeCleanupResultDetail
        Dim dr As DataRow
        For Each dr In dt.Rows
            If IsEvaluationOnly = False Then
                Try
                    'execute the change
                    myAD.SetUserProperty(dr("cn").ToString, CleanupSet.AttributeName, CleanupSet.NewValue)
                    iCleanCount += 1
                    isCleaned = True
                Catch ex As Exception
                    cleanComments = ex.ToString
                    isCleaned = False
                End Try
            Else
                isCleaned = False
                cleanComments = "Evaluation Only"
            End If

            'store the details
            acrd = New AttributeCleanupResultDetail(CleanupSet, dr("cn").ToString)
            acrd.IsCleaned = isCleaned
            acrd.CleanComments = cleanComments
            _AttributeCleanupResultDetails.Add(acrd)
        Next

        'build a cleanup result
        Dim acr As AttributeCleanupResult = New AttributeCleanupResult(CleanupSet)
        acr.NumberFound = dt.Rows.Count
        acr.NumberCleaned = iCleanCount

        Return acr

    End Function

End Class
